//
//  CMDBHomeTopicViewContorller.swift
//  CMDB
//
//  Created by 曹桂祥 on 17/4/7.
//  Copyright © 2017年 CMDB. All rights reserved.
//

import UIKit

class CMDBHomeTopicViewContorller: CMDBBaseViewController,UINavigationControllerDelegate {
    
    
    var tableView    : CMDBHomeTopicTableView!
    
    var superNav     : CustomNavigationControllerViewController?
    
    private let kTitleViewH : CGFloat = 44

    var lastTrend : CMDBDynamicBean   = CMDBDynamicBean()
    
    override func setup() {
        
        super.setup()
        
        buildTableView()
        
    }
    
    func buildTableView(){
        
        let contentH     = CMDB_Screen_Height - KStatusBarHeight - KNavBarHeight - kTitleViewH - KTabBarHeight
        
        let contentFrame = CGRect(x: 0, y: 0, width: CMDB_Screen_Width, height: contentH)
        
        tableView                              = CMDBHomeTopicTableView(frame: contentFrame)
        tableView.separatorStyle               = .None
        tableView.showsVerticalScrollIndicator = false
        tableView.mDelegate                    = self
        tableView.controller                   = self
        tableView.backgroundColor              = UIColor.Hex6(0x000000, alpha: 0.12)
        
        view.addSubview(tableView)
        
    }
}

extension CMDBHomeTopicViewContorller{
    
    func onHeaderRefresh() {
        
        let dynameicListParam = CMDBHomeDynamicParam()
        
        dynameicListParam.otype = 0
        
        dynameicListParam.ptime = "0"
        
        do{
            try CMDBDynamicManager.getInstance.queryHomeDynamicList(dynameicListParam, tableView: tableView, handler: { (homeDynamic) in
                
                    self.tableView.updateBannerData(homeDynamic.ads!)
                
                    self.tableView.updateDataWithOutReload(homeDynamic.trends)
                
                    self.tableView.updateCellAdpaters(homeDynamic.trends!, isInsert: false)
                
                    self.lastTrend = (homeDynamic.trends?.last)!
                
                }, error: { (rescode) in
                    
                }, failure: {
                    
            })
        } catch {
            
        }
    }
    
    func onFooterRefresh() {
        
//        let dynameicListParam = CMDBHomeDynamicParam()
//        
//        dynameicListParam.otype = 1
//        
//        dynameicListParam.ptime = self.lastTrend.createTime
//        
//        do{
//            try CMDBDynamicManager.getInstance.queryHomeDynamicList(dynameicListParam, tableView: tableView, handler: { (homeDynamic) in
//                
//                self.tableView.appendData(homeDynamic.trends)
//                
//                self.tableView.updateCellAdpaters(homeDynamic.trends!, isInsert: false)
//                
//                }, error: { (rescode) in
//                    
//                }, failure: {
//                    
//            })
//        } catch {
//            
//        }
    }
    
}


extension CMDBHomeTopicViewContorller{
    
    override func onItemClickType(itemClickType: String?, data: AnyObject?, indexPath: NSIndexPath?) {
        
        let itemType = ItemClickType(string:itemClickType!)
        
        switch itemType {
            
        case .TapOnView :
            
            let urls = data as! [CMDBPictureMdeol]
            
            var picURLs = [NSURL]()
            
            for (_,pictureModel) in urls.enumerate() {
                
                picURLs.append(NSURL(string: pictureModel.url)!)
            }
            
            // 1.创建控制器
           let photoBrowserVc = CMDBPhotoBrowserController(indexPath: indexPath!, picURLs: picURLs, picModels: urls)
            
            // 2.以modal的形式弹出控制器
            presentViewController(photoBrowserVc, animated: true, completion: nil)
            
        case .HomeTopicLike:
            
            do{
                
                let likeParam = CMDBFansParam()
                
                likeParam.tid = data as! String
                
                try CMDBFansManager.getInstance.fans(likeParam, handler: { (isSuccess) in
                    
                    if isSuccess{
                        
                        
                    
                    }
                    
                    }, error: { (rescode) in
                        
                    }, failure: {
                        
                })
            }catch{
                
            }

            
            
            break
        case .TableViewCellClick:
            
            let topicData = data as! CMDBDynamicBean
            
            let topicDetailViewController = CMDBTopicDetailViewController()
            
            topicDetailViewController.tid = topicData.tid
            
            topicDetailViewController.hidesBottomBarWhenPushed = true
            
            topicDetailViewController.dataBlock = {(string,count,_) in
                
                let typeString = string as! String
                
                if typeString == "comment"  {
                    var commentTotalCount = count as! Int
                    
                    commentTotalCount     = commentTotalCount + Int(topicData.commentCount)!
                    
                    topicData.commentCount = "\(commentTotalCount)"

                }else if typeString == "like"{
                    
                    var liekTotalCount = count as! Int
                    
                    liekTotalCount     = liekTotalCount + Int(topicData.likeCount)!
                    
                    if topicData.isLike   != "1" {
                        topicData.isLike = "1"
                    }
                    
                    topicData.likeCount  = "\(liekTotalCount)"
                    
                }else if typeString == "collect"{
                    
                    topicData.isCollect = "1"
                    
                }else if typeString == "unCollect"{
                    
                    topicData.isCollect = "0"
                    
                }else if typeString == "unLike"{
                    
                    let likeTotalCount     = Int(topicData.likeCount)! - 1
                    
                    if topicData.isLike   == "1" {
                        topicData.isLike = "0"
                    }
                    
                    if likeTotalCount < 0 {
                        topicData.likeCount = "0"
                    }
                    
                    topicData.likeCount = "\(likeTotalCount)"
                }
                
                let updataIndexPathArr = [NSIndexPath(forRow: indexPath!.row, inSection: indexPath!.section)]
                
                self.tableView.reloadRowsAtIndexPaths(updataIndexPathArr, withRowAnimation: .Fade)
                
            }
        
        let naav = self.navigationController! as! CustomNavigationControllerViewController
            
        naav.pushViewController(topicDetailViewController, animated: true, isForbiwDraw: true)
        
            
        default:
            printLog("default")
        }
        
    }
}

extension CMDBHomeTopicViewContorller{
    
    func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool {
        return true
    }
    
    
}


